package com.YCH.user.service.Impl;

import com.YCH.model.article.pojos.TbAuthor;
import com.YCH.model.behavior.dtos.FollowBehaviorDto;
import com.YCH.model.common.dtos.ResponseResult;
import com.YCH.model.common.enums.AppHttpCodeEnum;
import com.YCH.model.mappers.app.TbAuthorMapper;
import com.YCH.model.mappers.app.TbUserFanMapper;
import com.YCH.model.mappers.app.TbUserFollowMapper;
import com.YCH.model.mappers.app.TbUserMapper;
import com.YCH.model.user.dtos.UserRelationDto;
import com.YCH.model.user.pojos.TbUser;
import com.YCH.model.user.pojos.TbUserFan;
import com.YCH.model.user.pojos.TbUserFollow;
import com.YCH.user.service.AppFollowBehaviorService;
import com.YCH.user.service.AppUserRelationService;
import com.YCH.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
@Log4j2
@SuppressWarnings("all")
public class AppUserRelationServiceImpl implements AppUserRelationService {

    @Autowired
    TbUserFollowMapper tbUserFollowMapper;
    @Autowired
    TbUserFanMapper tbUserFanMapper;
    @Autowired
    TbAuthorMapper tbAuthorMapper;
    @Autowired
    TbUserMapper tbUserMapper;
    @Autowired
    AppFollowBehaviorService appFollowBehaviorService;

    /**
     * 关注/取消一个人
     * @param dto
     * @return
     */
    public ResponseResult follow(UserRelationDto dto){
        if(dto.getOperation()==null||dto.getOperation()<0||dto.getOperation()>1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"无效的operation参数");
        }
        // 获取到authorId（作者id）
        Long followId = dto.getUserId();
        if(followId==null && dto.getAuthorId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"followId或authorId不能为空");
        }else if(followId==null) {
            TbAuthor tbAuthor = tbAuthorMapper.selectById(dto.getAuthorId());
            if(tbAuthor!=null) {
                followId = tbAuthor.getUserId();
            }
        }
        if(followId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"关注人不存在");
        }else {
            // 判断当前用户是否已经关注
            TbUser user = AppThreadLocalUtils.getUser();
            if(user!=null) {
                if(dto.getOperation()==0) {
                    // 关注操作 保存ap_user_follow ap_user_fan 保存关注的行为
                    return followByUserId(user, followId, dto.getArticleId());
                }else{
                    // 取消关注 删除ap_user_follow ap_user_fan
                    return followCancelByUserId(user,followId);
                }
            }else{
                return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            }
        }
    }

    /**
     * 处理关注逻辑
     * @param user
     * @param authorId
     * @return
     */
    private ResponseResult followByUserId(TbUser user, Long authorId, Long articleId){
        // 判断用户是否存在
        TbUser followUser = tbUserMapper.selectById(authorId);
        if(followUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"关注用户不存在");
        }
        // APP用户关注信息表
        TbUserFollow tbUserFollow = tbUserFollowMapper.selectByFollowId(user.getId(),authorId);
        if(tbUserFollow==null){
            /**
             * 参数一：分组id
             * 参数二：被关注人的id
             * 参数三：粉丝id 当前登录人id
             */
            // APP用户粉丝信息表
            TbUserFan tbUserFan = tbUserFanMapper.selectByFansId(authorId, user.getId());
            if (tbUserFan == null) {
                TbUser tbUser = new TbUser();
                tbUser.setId(user.getId());
                tbUserFan = new TbUserFan();
                tbUserFan.setUserId(authorId);
                tbUserFan.setFans(tbUser);
                tbUserFan.setFansName(user.getUsername());
                tbUserFan.setLoyalteLevel((short) 0);
                tbUserFan.setIsDisplay(true);
                tbUserFan.setIsShieldComment(false);
                tbUserFan.setIsShieldLetter(false);
                tbUserFanMapper.insert(tbUserFan);
            }
            tbUserFollow = new TbUserFollow();
            tbUserFollow.setUserId((int) user.getId());//当前登陆人id
            TbUser tbUser = new TbUser();
            tbUser.setId(authorId);
            tbUserFollow.setFollow(tbUser);//被人关注的id
            tbUserFollow.setFollowName(followUser.getUsername());
            tbUserFollow.setCreatTime(new Date());
            tbUserFollow.setLoyalteLevel((short) 0);
            tbUserFollow.setIsNotice(true);

            // 记录关注行为
            FollowBehaviorDto dto = new FollowBehaviorDto();
            dto.setFollowId(authorId);
            dto.setArticleId(articleId);
            appFollowBehaviorService.saveFollowBehavior(dto);
            int insert = tbUserFollowMapper.insert(tbUserFollow);
            return ResponseResult.okResult(insert);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已关注");
        }
    }

    /**
     * 处理取消关注逻辑
     * @param user
     * @param authorId
     * @return
     */
    private ResponseResult followCancelByUserId(TbUser user, Long authorId){

        TbUserFollow auf = tbUserFollowMapper.selectByFollowId(user.getId(),authorId);
        if(auf==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"未关注");
        }else{
            TbUserFan fan = tbUserFanMapper.selectByFansId(authorId, user.getId());
            if (fan != null) {
                tbUserFanMapper.deleteByFansId(authorId, user.getId());
            }
            return ResponseResult.okResult(tbUserFollowMapper.deleteByFollowId(user.getId(),authorId));
        }
    }
}
